/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.util.Random;

import junit.framework.TestCase;





/**
 * Performs Validation Test for url validations.
 *
 * @version $Revision: 1128446 $ $Date: 2011-05-27 13:29:27 -0700 (Fri, 27 May 2011) $
 */
public class UrlValidatorTest extends TestCase {

   public UrlValidatorTest(String testName) {
      super(testName);
   } 
   
   // Here are two random testers:
   
   public void testRandomIsValid()
   {
	   //Test URLs we expect to be true
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   String base;
	   base = "google";
		   
	   String[] prefix = {"http://", "https://", "http://www.", "https://www.", "www.", ""};
		   
	   String[] suffix = {".com", ".org", ".edu", ".net", ".gov", ".mil", ".co.ca", ".co.uk", ".co.jp",".ca", ".uk", ".jp" };
		  
	   for(int g = 0; g < 1000; g++){
		   int pindex = randInt(0, prefix.length);
		   int sindex = randInt(0, suffix.length);
		   String testURL;
		   testURL = prefix[pindex] + base + suffix[sindex];
		   assertTrue(urlVal.isValid(testURL));
		   
	   }
  }
   
   public void testRandomIsNotValid()
   {
	   //Test URLs we expect to be false
   	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   String base;
	   base = "google";
	   
	   String[] prefix = {"htp://", "http://ww.", "ww."};
	   
	   String[] suffix = {".", ".dfgdf", "//", ""};
	   
	   for(int g = 0; g < 1000; g++){
		   int pindex = randInt(0, prefix.length);
		   int sindex = randInt(0, suffix.length);
		   String testURL;
		   testURL = prefix[pindex] + base + suffix[sindex];
		   assertFalse(urlVal.isValid(testURL));
		   
	   }
   } 
   
   /** Source: http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java
    * Returns a pseudo-random number between min and max, inclusive.
    * The difference between min and max can be at most
    * <code>Integer.MAX_VALUE - 1</code>.
    *
    * @param min Minimum value
    * @param max Maximum value.  Must be greater than min.
    * @return Integer between min and max, inclusive.
    * @see java.util.Random#nextInt(int)
    */
   public static int randInt(int min, int max) {

       // Usually this should be a field rather than a method variable so
       // that it is not re-seeded every call.
       Random rand = new Random();

       // nextInt is normally exclusive of the top value,
       // so add 1 to make it inclusive
       int randomNum = rand.nextInt((max - min) + 1) + min;

       return randomNum;
   }
}
